<?php
// 1. 连接mysql  选择数据库
@header("Content-Type:text/json;charset=utf-8");
@header("Access-Control-Allow-Origin:*");
$conn = mysqli_connect("b-sjisyki6cozhtk.bch.rds.gz.baidubce.com:3306", "b_sjisyki6cozhtk", "12345678", "b_sjisyki6cozhtk");

if (!$conn) { // 如果连接失败 => 脚本在当前退出
    exit('{"status":false,"message":"数据库链接失败"}');
}
mysqli_query($conn, "set names utf8");
mysqli_query($conn, "set character set utf-8"); // 向数据库存数据

if (!isset($_GET["id"])) {
    exit('{"status":false,"message":"请传入完整参数"}');
}

$id = $_GET["id"];
$sql = "select	 
s.title,s.img,s.money,g.number,g.id
from 
`sheet1` as s 
join 
`idbiao` as g
on 
s.id = g.g_id
where 
g.user = '$id'";
$result = mysqli_query($conn, $sql);

// print_r($result);
// php 对象取值   箭头语法
// echo $result->num_rows;  // 返回查询的数据数量

$obj = array();
if ($result) { // 有结果对象 => 可能有数据

    // 循环解析  => 先解析,将解析的结果赋值给变量$item ,并将其作为while循环的条件
    $all = [];
    while ($item = mysqli_fetch_assoc($result)) {  // 解析成功 =>  返回关联数组  解析失败=>结束循环
        array_push($all, $item);
    }
    if (count($all)) {
        $obj["status"] = true;
        $obj["message"] = "OK";
        $obj["list"] =  $all;
    } else {
        $obj["status"] = true;
        $obj["message"] = "暂无数据!";
        $obj["list"] =  $all;
    }
} else {
    $obj["status"] = false;
    $obj["message"] = "sql语句有误";
    $obj["sql"] = $sql;
}

echo json_encode($obj);
